package com.hp.bon.logreverse.start;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

public class LogTool
{
  public static void installSLF4JBridge()
  {
    SLF4JBridgeHandler.install();
    Handler consolHandler = null;
    java.util.logging.Logger logger = java.util.logging.Logger.getLogger("");
    for (Handler hand : logger.getHandlers()) {
      if ((hand instanceof ConsoleHandler)) {
        consolHandler = hand;
        break;
      }
    }
    if (consolHandler != null) {
      logger.removeHandler(consolHandler);
    }

    org.slf4j.Logger log = LoggerFactory.getLogger("com.hp.ocs.diameter");
    if (log.isTraceEnabled())
      java.util.logging.Logger.getLogger("com.hp.ocs.diameter").setLevel(Level.ALL);
    else if (log.isDebugEnabled())
      java.util.logging.Logger.getLogger("com.hp.ocs.diameter").setLevel(Level.FINE);
    else if (log.isInfoEnabled())
      java.util.logging.Logger.getLogger("com.hp.ocs.diameter").setLevel(Level.INFO);
    else {
      java.util.logging.Logger.getLogger("com.hp.ocs.diameter").setLevel(Level.WARNING);
    }
    java.util.logging.Logger ocsLoger = java.util.logging.Logger.getLogger("com.hp.ocs.diameter");
    ocsLoger.log(ocsLoger.getLevel(), "set java.util.logging.Logger success");
  }

  public static StringBuilder readFileByLines(String fileName)
  {
    StringBuilder sb = new StringBuilder();

    Reader read = null;
    try
    {
      read = new InputStreamReader(new FileInputStream(fileName), "UTF-8");

      BufferedReader reader = new BufferedReader(read);
      String line;
      while ((line = reader.readLine()) != null)
      {
        sb.append(line + "\r\n");
      }
    }
    catch (Exception e1) {
      e1.printStackTrace();

      if (read != null)
        try {
          read.close();
        }
        catch (IOException localIOException)
        {
        }
    }
    finally
    {
      if (read != null)
        try {
          read.close();
        }
        catch (IOException localIOException1) {
        }
    }
    return sb;
  }

  public static Properties loadPropertyFormFile(String propFile)
    throws IOException
  {
    InputStream propStream = null;
    Properties props = new Properties();
    propStream = new FileInputStream(propFile);
    props.load(propStream);
    if (propStream != null) {
      try {
        propStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return props;
  }

  public static void loadProperty(String propFile)
  {
    InputStream propStream = null;
    Properties props = new Properties();
    try {
      propStream = new FileInputStream(propFile);
      props.load(propStream);
      Properties sysProps = System.getProperties();
      sysProps.putAll(props);
      System.setProperties(sysProps);
    } catch (IOException e1) {
      throw new RuntimeException("load err:" + e1);
    } finally {
      if (propStream != null)
        try {
          propStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
}